<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\DictionaryTypeModel;
use Illuminate\Http\Request;
use App\Models\DictionaryModel;
use Illuminate\Support\Facades\Validator;

class Dictionary extends Controller
{

    public function types(Request $request) {
        $name = $request->input('name');

        $query = DictionaryTypeModel::query();

        if($name) {
            $query->where('name', 'like', "%{$name}%");
        }

        $items = $query->get();

        return $this->success($items);
    }

    public function list(Request $request){
        $id = $request->input('id', 0);
        $name = $request->input('name', '');
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 10);

        $query = DictionaryModel::query();
        if($id) {
            $query->where('type_id', $id);
        }

        if($name) {
            $query->where('name', 'like', "%name%");
        }

        $total = $query->count('id');
        $items = $query
            ->offset($page * $limit - $limit)
            ->limit($limit)
            ->get();

        $list = $items->map(function($item){
            $res = [
                'id' => $item->id,
                'dictionary_type_id' => $item->type_id,
                'dictionary_type_name' => $item->type ? $item->type->name : '',
                'name' => $item->name,
                'name_en' => $item->name_en,
                'system' => $item->system,
                'admin_id' => $item->admin_id,
                'admin_name' => $item->admin ? $item->admin->account : '',
                'created_at' =>  strval($item->created_at),
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    public function add(Request $request) {
        $auth = $request->get('auth');
        $params = $request->all();
        $validator = Validator::make($params, [
            'dictionary_type_id' => 'required|integer',
            'name' => 'required',
        ], [
            'dictionary_type_id.required' => '请选择字典类型',
            'name.required' => '请填写名称',
        ]);

        if ($validator->fails()) {
            $err = $validator->errors()->first();
            return $this->error($err);
        }

        $type = DictionaryTypeModel::find($params['dictionary_type_id']);
        if(!$type) {
            return $this->error('字典类型错误');
        }

        $dic = new DictionaryModel();
        $dic->type_id = $params['dictionary_type_id'];
        $dic->name = $params['name'];
        $dic->name_en = $params['name_en'] ?? '';
        $dic->system = DictionaryModel::NO_SYSTME;
        $dic->admin_id = $auth->id;
        $dic->save();
        return $this->success();
    }

    public function update(Request $request, int $id) {
        $params = $request->all();
        $validator = Validator::make($params, [
            'dictionary_type_id' => 'required|integer',
            'name' => 'required',
        ], [
            'dictionary_type_id.required' => '请选择字典类型',
            'name.required' => '请填写名称',
        ]);

        if ($validator->fails()) {
            $err = $validator->errors()->first();
            return $this->error($err);
        }

        $dic = DictionaryModel::find($id);
        if(!$dic) {
            return $this->error('字典项错误');
        }

        if($dic->system == DictionaryModel::SYSTME) {
            return $this->error('不能修改系统内置字典');
        }

        $dic->type_id = $params['dictionary_type_id'];
        $dic->name = $params['name'];
        $dic->name_en = $params['name_en'] ?? '';
        $dic->save();

        return $this->success();
    }

    public function del(int $id) {
        $dic = DictionaryModel::find($id);
        if(!$dic) {
            return $this->error('字典项错误');
        }
        if($dic->system == DictionaryModel::SYSTME) {
            return $this->error('不可删除系统内置字典');
        }
        $dic->delete();
        return $this->success();
    }
}
